package com.navinfo.opentsp.platform.computing.analysis.util;

import org.apache.spark.Partitioner;

/**
 * Created by neo on 2017/7/7.
 */
public class TilePartitioner extends Partitioner {

    private int partitionNum = 1000;
    public TilePartitioner(){}

    public TilePartitioner(int partitionNum){
        this.partitionNum = partitionNum;
    }

    @Override
    public int numPartitions() {
        return this.partitionNum;
    }

    @Override
    public int getPartition(Object key) {

        try {
            Long tile1 = VehiclePassTimesQuadtree.getParentTileId(Long.parseLong(key.toString()));
            Long tile2 = VehiclePassTimesQuadtree.getParentTileId(tile1);
            return (int)(tile2 % partitionNum);
        } catch (NumberFormatException e){
            e.printStackTrace();
        }
        return 0;
    }
}
